#include <iostream>
using namespace std;

// 浅拷贝：简单的赋值操作，仅复制对象的成员变量值，对指针类型成员仅复制地址，导致两个对象指向同一块堆内存
// 深拷贝：对于 指针类 型成员，在拷贝时 重新申请(new) 独立的堆内存空间，并复制内容，避免多个对象共享同一块内存

class person {
    //初始化列表
public:
    person() : M_AGE(0) ,  M_HEIGHT(nullptr) {
        cout <<  "person 的默认构造函数调用" << endl;
    }
    person(int age , int height) : M_AGE(age) ,M_HEIGHT(new int(height)){
        cout << "person 的有参构造函数调用" << endl;
    }

    person(const person & p) : M_AGE(p.M_AGE) , M_HEIGHT(new int(*p.M_HEIGHT)) {
        cout << "person 的拷贝构造函数调用" << endl;
    }
    ~person() {
        if (M_HEIGHT != NULL) {
            delete M_HEIGHT;
            M_HEIGHT = NULL;
        }
        cout << "person 的析构函数调用" << endl;
    }

    int M_AGE;
    int * M_HEIGHT;
};
class Person {
public:
    Person() {
        cout << "Person 的默认构造函数调用" << endl;
    }
    Person(int age , int height) {
        cout << "Person 的有参构造函数调用" << endl;
        m_age = age ;
        m_Height = new int(height);
    }
    Person(const Person & p) {
        cout << "Person 的拷贝构造函数调用" << endl;
        m_age = p.m_age;
        m_Height = new int(*p.m_Height);
    }

    ~Person() {
        if (m_Height != NULL) {
            delete m_Height;
            m_Height = NULL;
        }
        cout << "Person 的析构函数调用" << endl;
    }

    int m_age ;
    int * m_Height;
};

void test() {
    Person p1(18,182);

    Person p2(p1);

    cout << "p1的年龄：" << p1.m_age << "，p1的身高：" << *p1.m_Height << endl;
    cout << "p2的年龄：" << p2.m_age << "，p2的身高：" << *p2.m_Height << endl;
}

void test1() {
    person p1(18,182);

    person p2(p1);

    cout << "p1的年龄：" << p1.M_AGE << "，p1的身高：" << *p1.M_HEIGHT << endl;
    cout << "p2的年龄：" << p2.M_AGE << "，p2的身高：" << *p2.M_HEIGHT << endl;
}
int main() {
    test1();
    return 0;
}